/**
 * 
 */
package com.rodirod.vrmonitor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;


/**
 * @author Rodolphe Rousselet
 *
 */
public class RaceInfo implements Constants {

	private static final String TAG = "RegateInfo";

	/**
	 * List of Map containing at least these couples :
	 *      M_TITLE : name of a property
	 *      M_VALUE : value of the property
	 * This Map value will be completed by a SimpleAdapter for a ListActivity
	 */
	private List<Map<String, Object>> dataAdapter_ = null;
	// map which key is the title of the property and the value is the same as above (shared)
	private Map< String , Map<String,Object> > dataMap_ = null;
	
	// url to read to retrieve data
	private String urlPath_ = "";

	public RaceInfo(String name, String urlPath) {
		// TODO : utiliser name pour charger des param�tres les derni�res valeurs
		dataAdapter_ = new ArrayList< Map<String,Object> >();
		dataMap_ = new HashMap<String, Map<String,Object>>();
		urlPath_ = urlPath;
	}
	
	/**
	 *  update the current value or create the field
	 * @param fieldName
	 * @param fieldValue
	 * @return the alarmState of the field
	 */
	public void setField(String fieldName, String fieldValue) {
		Map<String,Object> data = dataMap_.get(fieldName);
		if (null == data) {
			// create the object
			data = new HashMap<String,Object>();
			data.put(M_TITLE, fieldName);
			data.put(M_VALUE, fieldValue);
			data.put(M_VALUE_REF, fieldValue);
			data.put(M_VALUE_LAST, fieldValue);
			data.put(M_MONITOR, I_MONITORED_NONE);
			data.put(M_ALARM_STATE, I_ALARM_OFF);

			dataMap_.put(fieldName, data);
			dataAdapter_.add(data);
		} else {
			data.put(M_VALUE_LAST, data.get(M_VALUE));
			data.put(M_VALUE, fieldValue);
		}
	}
	
	/**
	 * Compute the alarm of the data
	 * @param data
	 * @return alarmState
	 */
	public int updateAlarmStates() {

		int globalAlarmState = I_ALARM_OFF;

		for (int i=0 ; i<dataAdapter_.size() ; i++) {
			Map<String,Object> map = dataAdapter_.get(i);

			String currentValue = (String)map.get(M_VALUE);
			String refValue = (String)map.get(M_VALUE_REF);		
			int valMonitor = (Integer)map.get(M_MONITOR);
			int alarmState = (Integer)map.get(M_ALARM_STATE);

			switch (valMonitor) {
			case I_MONITORED_NONE:
			case I_MONITORED_UP:
			case I_MONITORED_DOWN:
				break;
			case I_MONITORED_ALARM:
				if (currentValue.equals(refValue)) {
					alarmState = I_ALARM_OFF;
				} else {
					alarmState = I_ALARM_ON;
				}
				break;
			default:
				break;
			}
			map.put(M_ALARM_STATE, alarmState);
			if (alarmState==I_ALARM_ON) globalAlarmState=I_ALARM_ON;
		}
		return globalAlarmState;
	}

	// update the ref value for all the fields with the current ones
	public void storeRefValues() {
		for (int i=0 ; i<dataAdapter_.size() ; i++) {
			Map<String,Object> e = dataAdapter_.get(i);
			e.put(M_VALUE_REF,e.get(M_VALUE));
		}
	}
	
	/**
	 * write data in a String with the format "key : value"
	 */
	public String toString() {
		StringBuffer result = new StringBuffer(urlPath_);
		result.append("\n");
		
		for (int i=0 ; i<dataAdapter_.size(); i++) {
			Map<String,Object> e = dataAdapter_.get(i);
			result.append(e.get(M_TITLE) + " : " + e.get(M_VALUE) + "\n");
		}
		return result.toString();
	}

	public List<Map<String, Object>> getAdapterViewData() {
		return dataAdapter_;
	}

	/**
	 * 
	 * @param urlPath
	 * @return true if read ok, false if any error occurs
	 */
	public boolean readDataFromUrl() {
		boolean result = true;
		
		UrlReader urlReader = new UrlReader(urlPath_);
		try {
			urlReader.fillData(this);
		} catch (IOException e) {
			Log.v(TAG,e.getMessage());
			result = false;
		}
		return result;
	}
}
